class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        roman_map = {
        'I': 1,
        'V': 5,
        'X': 10,
        'L': 50,
        'C': 100,
        'D': 500,
        'M': 1000
        }
        
        total = 0
        post_value = 0
        
        for i in range(len(s)):
            if i < len(s) - 1:
                current_val = roman_map[s[i]]
                post_value = roman_map[s[i+1]]
                if current_val < post_value:
                    total -= current_val
                else:
                    total += current_val 
            elif i == len(s) - 1:
                total += roman_map[s[i]]
        return total
if __name__ == '__main__':
    s = "MCMXCIV"   
    print(Solution().romanToInt(s))

# 思路：
# 1.创建一个字典来映射罗马数字字符到对应的数值
# 2.遍历罗马数字字符串
# 3.如果当前字符代表的值小于下一个字符代表的值，则减去当前值
# 4.否则，加上当前值